package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.JwtUtils;
import com.itheima.common.PageBean;
import com.itheima.dto.EmpExprDto;
import com.itheima.dto.LoginDto;
import com.itheima.dto.QueryEmpDto;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import com.itheima.pojo.EmpExpr;
import com.itheima.service.EmpService;
import com.itheima.vo.EmpVo;
import com.itheima.vo.LoginVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;

    @Override
    public PageBean pageQuery(QueryEmpDto dto) {
//
//        dto.setPage((dto.getPage()-1)*dto.getPageSize());
//        List<Emp> list = empMapper.list(dto);
//
//        Long total = empMapper.countTotal(dto);
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<EmpVo> list = empMapper.list(dto);
        PageBean pageBean = new PageBean(list.getTotal(), list.getResult());
        return pageBean;
    }

    @Override
    public void add(EmpExprDto dto) {
        Emp emp = new Emp();
        BeanUtils.copyProperties(dto, emp);
        emp.setPassword("123456");
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.insert(emp);
        List<EmpExpr> empExprList = dto.getExprList();
        empExprList.forEach(empExpr -> {
            empExpr.setEmpId(emp.getId());
            empExprMapper.insert(empExpr);
        });


    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Override
    public EmpVo getById(Integer id) {
        EmpVo vo = new EmpVo();
        Emp emp = empMapper.getById(id);
        List<EmpExpr> exprList = empExprMapper.getByEmpId(id);
        BeanUtils.copyProperties(emp, vo);
        vo.setExprList(exprList);
        return vo;
    }

    /**
     * 修改员工
     *
     * @param dto
     */
    @Override
    public void updataById(EmpExprDto dto) {
        Emp emp = new Emp();
        BeanUtils.copyProperties(dto, emp);
        empMapper.updateById(emp);
        empExprMapper.DeleteByEmpId(emp.getId());
        List<EmpExpr> exprList = dto.getExprList();
        exprList.forEach(empExpr -> {
            empExpr.setEmpId(emp.getId());
            empExprMapper.insert(empExpr);
        });

    }

    /**
     * 根据id删除员工
     *
     * @param ids
     */
    @Override
    public void deleteByIds(Integer[] ids) {
        for (Integer id : ids) {
            empMapper.deleteByIds(id);
            empExprMapper.DeleteByEmpId(id);
        }
    }

    /**
     * 查询所有员工
     *
     * @return
     */
    @Override
    public List<Emp> getAll() {
        List<Emp> emp = empMapper.getAll();
        return emp;
    }

    /**
     * 登陆
     *
     * @param dto
     * @return
     */
    @Override
    public LoginVo login(LoginDto dto) {
        Emp emp = empMapper.getByUserNameAndPassword(dto);
        if (emp == null) {
            throw new RuntimeException("登陆失败");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("id", emp.getId());
        String jwt = JwtUtils.generateJwt(map);


        return LoginVo.builder().id(emp.getId()).name(emp.getName()).username(emp.getUsername()).token(jwt).build();
    }
}
